package com.dhec.ptms.epr.db;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.dhec.ptms.epr.model.SampleTestScores;
import com.ionglobal.db.DBI;
import com.ionglobal.lib.SysException;

public class SampleTestScoresDB {

	private static final Logger logger = LoggerFactory
			.getLogger(SampleTestScoresDB.class);

	public void handleData(DBI dbi, SampleTestScores bean) throws SysException {
		bean.setBenchMark(dbi.rsGetString("benchMark"));
		bean.setCheckItem(dbi.rsGetString("checkItem"));
		bean.setCheckMethod(dbi.rsGetString("checkMethod"));
		bean.setId(dbi.rsGetLong("id"));
		bean.setRemark(dbi.rsGetString("remark"));
		bean.setReportid(dbi.rsGetInt("reportid"));
		bean.setTestScore(dbi.rsGetString("testScore"));
		bean.setType(dbi.rsGetString("type"));
	}

	public boolean save(DBI dbi, SampleTestScores bean) {
		try {
			String sql = "INSERT INTO EPR_SampleTestScores (id, reportid, checkItem, checkMethod, type, benchMark, testScore, remark) VALUES ("
					+ bean.getId()
					+ ", "
					+ bean.getReportid()
					+ ", '"
					+ dbi.fixSql(bean.getCheckItem())
					+ "', '"
					+ dbi.fixSql(bean.getCheckMethod())
					+ "', '"
					+ dbi.fixSql(bean.getType())
					+ "', '"
					+ dbi.fixSql(bean.getBenchMark())
					+ "', '"
					+ dbi.fixSql(bean.getTestScore())
					+ "', "
					+ (bean.getRemark() != null ? "'"+dbi.fixSql(bean
							.getRemark())+"'" : "null") + ")";
			sql = trunc(sql);
			
			return dbi.execute(sql);
		} catch (SysException e) {
			logger.error(
					"SampleTestScoresDB.save(DBI, SampleTestScores) Error!", e);
		}

		return false;

	}

	public boolean update(DBI dbi, SampleTestScores bean) {
		try {
			String sql = "UPDATE EPR_SampleTestScores SET reportid = "
					+ bean.getReportid()
					+ ", checkItem = '"
					+ dbi.fixSql(bean.getCheckItem())
					+ "', checkMethod = '"
					+ dbi.fixSql(bean.getCheckMethod())
					+ "', type = '"
					+ dbi.fixSql(bean.getType())
					+ "', benchMark = '"
					+ dbi.fixSql(bean.getBenchMark())
					+ "', testScore = '"
					+ dbi.fixSql(bean.getTestScore())
					+ "', remark = "
					+ (bean.getRemark() != null ? "'"+dbi.fixSql(bean.getRemark())+"'"
							: "null") + " WHERE id = " + bean.getId();
			sql = trunc(sql);
			
			return dbi.execute(sql);
		} catch (SysException e) {
			logger.error("ReferBenchMarkDB.update(DBI, ReferBenchMark) Error!",
					e);
		}

		return false;

	}
	
	public boolean clearScore(DBI dbi, int reportid) {
		try {
			return dbi.execute("delete from EPR_SampleTestScores where reportid = "+reportid);
		} catch (SysException e) {
			logger.error("ReferBenchMarkDB.update(DBI, ReferBenchMark) Error!",
					e);
		}

		return false;

	}
	
	public List<SampleTestScores> list(DBI dbi, int reportid) throws SysException{
		List<SampleTestScores> list = new ArrayList<SampleTestScores>(0);
		dbi.execQuery("select * from EPR_SampleTestScores where reportid="+reportid+" order by id asc");
		while(dbi.rsNext()){
			SampleTestScores bean = new SampleTestScores();
			this.handleData(dbi, bean);
			list.add(bean);
		}
		dbi.rsClose();
		
		return list;
	}
	
	private String trunc(String str){
		/*String tmp;
		try {
			if(str!=null)
				tmp = new String(str.getBytes("GBK"), "ISO-8859-1");
			else
				tmp = str;
		} catch (UnsupportedEncodingException e) {
			return str;
		}*/
		return str;
	}
}
